import pygame

from pygame.locals import *
from Constants import *
from Block import Block

BLOCK_ICON_WIDTH = 20
BLOCK_ICON_HEIGHT = 20
BLOCK_ICON_SPACING = 2

def iconSurface(color):
    surface = pygame.Surface((BLOCK_ICON_WIDTH, BLOCK_ICON_HEIGHT));
    surface.fill(color)
    return surface

class Puzzle:
    puzzleBlockIcons = {}
    puzzleBlockIcons['Stone Block'] = iconSurface((200,200,200))
    puzzleBlockIcons['Grass Block'] = iconSurface((50,250,50))
    puzzleBlockIcons['Dirt Block'] = iconSurface((100,100,50))
    puzzleBlockIcons['Water Block'] = iconSurface((50, 20, 200))
    
    
    def __init__(self, reward, owner):
        self.names = [['Grass Block','Grass Block','Grass Block'], 
                      ['Grass Block','Dirt Block', 'Grass Block'], 
                      ['Grass Block','Dirt Block','Stone Block'], 
                      ['Water Block','Water Block','Water Block']]
        self.createSurfaceIcon()
        self.reward = reward
        self.owner = owner

    def createSurfaceIcon(self):
        width = len(self.names)
        height = len(self.names[0])
        self.canvas = pygame.Surface((width * (BLOCK_ICON_WIDTH + BLOCK_ICON_SPACING), height * (BLOCK_ICON_HEIGHT + BLOCK_ICON_SPACING)))
        colorKey = (255,0,255)
        self.canvas.fill(colorKey)
        self.canvas.set_colorkey(colorKey)
        for x in xrange(width):
            for y in xrange(height):
                key = self.names[x][y]
                xPos = x * (BLOCK_ICON_WIDTH + BLOCK_ICON_SPACING)
                yPos = y * (BLOCK_ICON_WIDTH + BLOCK_ICON_SPACING)
                icon = Puzzle.puzzleBlockIcons[key]
                self.canvas.blit(icon,(xPos,yPos))
    
    def render(self, screen, pos):
        screen.blit(self.canvas, pos)
    
    def check(self, stacks, x, y):
        z = stacks[x][y].size()
        try:
            for i in xrange(len(self.names)):
                for j in xrange(len(self.names[i])):
                    mapX = x+i
                    mapY = y+j
                    stack = stacks[mapX][mapY]
                    if stack.size() != z or stack.blocks[z-1].name != self.names[i][j]:
                        return False
        except IndexError:
            print 'out of range'
            return False
        return True
    
    # this function adds the house to the map
    def puzzleReward(self, map, x, y):
        print 'Puzzle Solved'
        
        map.houses[x+1][y+1] = self.reward
        map.houses[x+2][y+1] = self.reward
        self.reward.setPos(map.getRenderingCoords((x+2, y+1)))
        fromPos = self.owner.pos
        toPos = (x+2,y+2)
        map.moveActor(fromPos, toPos)
        
        #lock stacks
        for column in map.stacks[x:x+len(self.names)]:
            for stack in column[y:y+len(self.names[0])]:
                stack.locked = True
        map.updateRow(x+1,y)
        map.updateRow(x+2,y)
        
        
    
class PuzzleHandler:
    def __init__(self):
        self.puzzleList = []
    
    def addPuzzle(self, puzzle):
        self.puzzleList.append(puzzle)
    
    def handleDrop(self, map, x, y):
        for puzzle in self.puzzleList:
            if puzzle.check(map.stacks, x, y):
                puzzle.puzzleReward(map,x,y)
                self.puzzleList.remove(puzzle)
                return puzzle